package com.zhanggm.zyk.auth.controller;


import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.jwt.JWTUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zhanggm.zyk.auth.config.AjaxResult;
import com.zhanggm.zyk.auth.entity.TUser;
import com.zhanggm.zyk.auth.service.ITUserService;
import com.zhanggm.zyk.auth.utils.ZhenziSmsUtils;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhanggm
 * @since 2024-05-24
 */
@RestController
@RequestMapping("/user")
public class TUserController {

    @Autowired
    ITUserService itUserService;

    @Autowired
    RedisTemplate redisTemplate;

    @GetMapping("/user/getUserById")
    public TUser getUserById(@RequestParam("id") Integer id){
        return itUserService.getById(id);
    }


    private String salt = "123";

    @GetMapping("login")
    public AjaxResult login(TUser user) {

        Object code = redisTemplate.opsForValue().get(user.getUuid());
        if (!user.getCode().equals(code)) {
            return AjaxResult.error("验证码错误");
        }
        LambdaQueryWrapper<TUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TUser::getUsername, user.getUsername());
        TUser loginUser = itUserService.getOne(queryWrapper);

        String md5Psswword = SecureUtil.md5(user.getPassword() + salt);
        System.out.println(md5Psswword);
        if (!md5Psswword.equals(loginUser.getPassword())) {
            return AjaxResult.error();
        }

        String token = JWTUtil.createToken(BeanUtil.beanToMap(loginUser), salt.getBytes());

        redisTemplate.opsForValue().set(token, loginUser, 1, TimeUnit.DAYS);

        return AjaxResult.success(token);


    }


    @GetMapping("getUserInfo")
    public User getUserInfo(@RequestParam("token") String token) {
        return (User) redisTemplate.opsForValue().get(token);
    }


    @Autowired
    HttpServletResponse response;


    @GetMapping("getCode")
    public void getCode(String uuid) throws IOException {
        //生成验证码
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100);
        String code = lineCaptcha.getCode();
        //缓存验证码
        redisTemplate.opsForValue().set(uuid, code, 1, TimeUnit.MINUTES);
        lineCaptcha.write(response.getOutputStream());
    }


    @Autowired
    ZhenziSmsUtils zhenziSmsUtils;


    @GetMapping("test")
    public AjaxResult test()  {
        zhenziSmsUtils.send("15669353326","1234","4分钟");
        return AjaxResult.success();
    }



}
